﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace 图书管理系统
{
    public partial class admin2 : Form
    {
        public admin2()
        {
            InitializeComponent();

        }

        private void admin2_Load(object sender, EventArgs e)
        {
            table();
        }
        //从数据库读取数据并显示在表格控件中
        public void table()
        {
            dataGridView1.Rows.Clear();//清空表格中已有数据
            SQLconnect sqlconnect = new SQLconnect();
            string sql = "select * from t_book";
            IDataReader dc = sqlconnect.read(sql);
            while (dc.Read())
            {
                dataGridView1.Rows.Add(dc[0].ToString(), dc[1].ToString(), dc[2].ToString(), dc[3].ToString(), dc[4].ToString());
            }
            dc.Close();
            sqlconnect.SQLclose();
        }
        //根据书号显示数据
        public void tableid()
        {
            dataGridView1.Rows.Clear();//清空表格中已有数据
            SQLconnect sqlconnect = new SQLconnect();
            string sql = $"select * from t_book where id='{textBox1.Text}'";
            IDataReader dc = sqlconnect.read(sql);
            while (dc.Read())
            {
                dataGridView1.Rows.Add(dc[0].ToString(), dc[1].ToString(), dc[2].ToString(), dc[3].ToString(), dc[4].ToString());
            }
            dc.Close();
            sqlconnect.SQLclose();
        }
        //根据书名显示数据 模糊查询
        public void tablename()
        {
            dataGridView1.Rows.Clear();//清空表格中已有数据
            SQLconnect sqlconnect = new SQLconnect();
            string sql = $"select * from t_book where name like '%{textBox2.Text}%'";
            IDataReader dc = sqlconnect.read(sql);
            while (dc.Read())
            {
                dataGridView1.Rows.Add(dc[0].ToString(), dc[1].ToString(), dc[2].ToString(), dc[3].ToString(), dc[4].ToString());
            }
            dc.Close();
            sqlconnect.SQLclose();
        }
        private void button3_Click(object sender, EventArgs e)
        {
            try
            {
                string id = dataGridView1.SelectedRows[0].Cells[0].Value.ToString();//获取书号
                label2.Text = id + dataGridView1.SelectedRows[0].Cells[1].Value.ToString();
                DialogResult dr = MessageBox.Show("确认删除？", "信息提示", MessageBoxButtons.OKCancel, MessageBoxIcon.Question);
                if (dr == DialogResult.OK)
                {
                    string sql = $"delete from t_book where id='{id}'";
                    SQLconnect sqlconnect = new SQLconnect();
                    if (sqlconnect.Execute(sql) > 0)
                    {
                        MessageBox.Show("删除成功");
                        table();
                    }
                    else
                    {
                        MessageBox.Show("删除失败" + sql);
                    }
                    sqlconnect.SQLclose();
                }
            }
            catch
            {
                MessageBox.Show("请在表格中选中要删除的项目", "信息提示", MessageBoxButtons.OK, MessageBoxIcon.Error);
            }
        }

        private void dataGridView1_Click(object sender, EventArgs e)
        {
            label2.Text = dataGridView1.SelectedRows[0].Cells[0].Value.ToString() + dataGridView1.SelectedRows[0].Cells[1].Value.ToString();
        }

        private void button1_Click(object sender, EventArgs e)
        {
            admin21 admin = new admin21();
            admin.ShowDialog();
        }

        private void button2_Click(object sender, EventArgs e)
        {
            try
            {
                string id = dataGridView1.SelectedRows[0].Cells[0].Value.ToString();
                string name = dataGridView1.SelectedRows[0].Cells[1].Value.ToString();
                string author = dataGridView1.SelectedRows[0].Cells[2].Value.ToString();
                string press = dataGridView1.SelectedRows[0].Cells[3].Value.ToString();
                string number = dataGridView1.SelectedRows[0].Cells[4].Value.ToString();
                admin22 admin = new admin22(id, name, author, press, number);
                admin.ShowDialog();
                table();//刷新数据
            }
            catch
            {
                MessageBox.Show("图书数据获取错误");
            }
        }

        private void button4_Click(object sender, EventArgs e)
        {
            table();
            textBox1.Text = "";
            textBox2.Text = "";
        }

        private void button6_Click(object sender, EventArgs e)
        {
            tableid();
        }

        private void button5_Click(object sender, EventArgs e)
        {
            tablename();
        }
    }
}
